package com.itrident.dialog;

import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.table.JBTable;
import com.itrident.data.BeanData;
import com.itrident.data.DataSource;
import com.itrident.msg.MessageUtil;
import com.itrident.process.DefaultProcess;
import com.itrident.process.Process;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class ItridentDiglog extends DialogWrapper {

    private static String[] COLUMN_NAME={"title","remark"};

    private JBTable jTable;

    private DefaultTableModel tableModel;

    private Process process = new DefaultProcess ();

    private String packagePath;

    private String path;

    private VirtualFile virtualFile;

    public ItridentDiglog(VirtualFile virtualFile, String packagePath, String path) {
        super (true);
        this.virtualFile = virtualFile;
        this.packagePath = packagePath;
        this.path = path;
        init();
        setTitle("itrdent");
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel content = new JPanel (new BorderLayout());
        content.setPreferredSize (new Dimension (650,350));
        tableModel = new DefaultTableModel (null,COLUMN_NAME){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        jTable = new JBTable ();
        jTable.setModel (tableModel);
        jTable.setEnabled (true);
        jTable.getTableHeader ().setReorderingAllowed (false);
        content.add (jTable.getTableHeader (),BorderLayout.NORTH);
        content.add (jTable,BorderLayout.CENTER);
        initData();
        return content;
    }

    private void initData() {
        for (BeanData beanData : DataSource.getBeanDatas ()) {
            tableModel.addRow (new String[]{beanData.getId (),beanData.getExplain ()});
        }
    }

    @Override
    protected void doOKAction() {
        int[] selectedRows = jTable.getSelectedRows ();
        if (selectedRows.length == 0) {
            Messages.showMessageDialog( "big bro,choice data", "warn",Messages.getInformationIcon());
            return;
        }
        for (int row : selectedRows) {
            String id = (String) jTable.getValueAt (row, 0);
            process.handle (id,virtualFile,path,packagePath);
        }
        MessageUtil.notifyMsg ("big bro,add success, please flash",MessageType.INFO);
        super.doOKAction ();
    }
}
